<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Model\Admin\Cat;
class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //

       $data = DB::table('goods')
            ->join('cat','goods.cat_id','=','cat.id')
            ->select('goods.*','cat.cat_name')
            ->get();
        return view('admin.goods_list',['goods'=>$data]);
       
       
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $data1 = DB::table('cat')->get();
        $data2 = Cat::tree($data1);
        return view('admin.goods_create',['cat'=>$data2]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //处理图片上传
       if ($request->hasFile('photo')) {
              $file = $request->file("photo");
              if($file->isValid()){
              //获取上传文件名的后缀名
              $ext = $file->getClientOriginalExtension();
              //执行移动上传文件
              $photo_path = "images/photo/";
              $filename = time().rand(10,99).".".$ext;
              $file->move($photo_path,$filename);
              $data['goods_photo']    = "/images/photo/".$filename;
           }
        }else{
            $data['goods_photo']    = "/images/photo/1.jpg";
        }
       //处理表单传来的数据
        $data['goods_name']     = $request->name;
        $data['goods_num']      = $request->num;
        $data['goods_price']    = $request->price;
        $data['goods_status']   = $request->status;
        $data['cat_id']         = $request->cat_id;
        $data['goods_des']      = $request->des;
        $data['new']            = $request->new;
        $data['hot']            = $request->hot;
        //插入数据库
        DB::table('goods')->insert($data);
        return redirect('/admin/goods');

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $data1 = DB::table('cat')->get();
        $data2 = Cat::tree($data1);
        $data = DB::table('goods')->where('id',$id)->first();
        return view('admin.goods_edit',['goods'=>$data,'cat'=>$data2]);
    }

    /**
     * Update the specified resource in storage'
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update1(Request $request, $id)
    {
         //处理图片上传
       if ($request->hasFile('photo')) {
              $file = $request->file("photo");
              if($file->isValid()){
              //获取上传文件名的后缀名
              $ext = $file->getClientOriginalExtension();
              //执行移动上传文件
              $photo_path = "images/photo/";
              $filename = time().rand(10,99).".".$ext;
              $file->move($photo_path,$filename);
              $data['goods_photo']    = "/images/photo/".$filename;
           }
        }
    
       //处理表单传来的数据
        $data['goods_name']     = $request->name;
        $data['goods_num']      = $request->num;
        $data['goods_price']    = $request->price;
        $data['goods_status']   = $request->status;
        $data['cat_id']         = $request->cat_id;
        $data['goods_des']      = $request->des;
        $data['new']            = $request->new;
        $data['hot']            = $request->hot;
        //插入数据库
        DB::table('goods')->where('id',$id)->update($data);
        return redirect('/admin/goods');
       
        
    }
    //产品下架
    public function down($id){
       DB::table('goods')->where('id',$id)->update(['goods_status'=>1]);
    }
     //产品下架
    public function up($id){
        DB::table('goods')->where('id',$id)->update(['goods_status'=>0]);
    }
    //产品删除
    public function del($id)
    {
        DB::table('goods')->where('id',$id)->delete();
        return response()->json(['status' => '200']);
    }

}
